package com.dog.framework.mybatis.utils;

import com.dog.framework.core.model.PageQuery;
import com.dog.framework.core.model.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * 分页工具类
 */
public class PageUtils {
    /**
     * 获取分页数据
     * @param param 请求参数
     */
    public static <T,P extends PageQuery> PageResult<T> getPage(P param, Consumer<P> consumer){
        Page<T> page = PageHelper.startPage(Objects.requireNonNull(Objects.requireNonNull(param).getPageNum()), Objects.requireNonNull(Objects.requireNonNull(param).getPageSize())).doSelectPage(() -> consumer.accept(param));
        PageResult<T> data = new PageResult<>();
        data.setRows(page.getResult());
        data.setPageNum(page.getPageNum());
        data.setPageSize(page.getPageSize());
        data.setTotal(page.getTotal());
        data.setPages(page.getPages());
        return data;
    }


    public static<T> PageResult<T> getPage(PageInfo page, List<T> list){
        PageResult<T> data = new PageResult<>();
        data.setRows(list);
        data.setPageNum(page.getPageNum());
        data.setPageSize(page.getPageSize());
        data.setTotal(page.getTotal());
        data.setPages(page.getPages());
        return data;
    }





}
